package helloworld.admin.form.teacher;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.ImageButtonBean;
import org.apache.struts.util.MessageResources;
import helloworld.example.util.BaseActionForm;

/**
 * @author TUYEN-DM
 *         <p>
 *         2009/05/09
 *         </p>
 *         <p>
 *         TeacherForm is the object representation of the html form for
 *         entering teacher's name in helloworld.
 *         </p>
 */
public class TeacherForm extends BaseActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Teacher Name */
	private String teacherName;
	private String address;
	private String phone;
	private String email;
	private String country;
	private int experiencedYears;

	private String photoUrl;
	private String birthDate;

	private boolean isExisting;

	private int teacherID;

	private ImageButtonBean save;
	private ImageButtonBean cancel;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public int getExperiencedYears() {
		return experiencedYears;
	}

	public void setExperiencedYears(int experiencedYears) {
		this.experiencedYears = experiencedYears;
	}

	public String getPhotoUrl() {
		return photoUrl;
	}

	public void setPhotoUrl(String photoUrl) {
		this.photoUrl = photoUrl;
	}

	public String getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;
	}

	public void setExisting(boolean isExisting) {
		this.isExisting = isExisting;
	}

	public TeacherForm() {
		// Dont forget to Initialize here
		// Also dont forget to init in reset();
		init();
	}

	// Without this method to initialization, non-primitive objects such as
	// Address, ImageButtonBean will throw NullPointerException
	// or might not work as expected
	// since a getAddress(), getSave() etc. returns a null object
	protected void init() {
		teacherName = "";
		address = "";
		birthDate = "";
		phone = "";
		experiencedYears = 0;
		photoUrl = "";
		country = "";
		birthDate = "";
		save = new ImageButtonBean();
		cancel = new ImageButtonBean();
		isExisting = false;
		teacherID = 0;
	}

	public int getTeacherID() {
		return teacherID;
	}

	public void setTeacherID(int i) {
		teacherID = i;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String s) {
		this.teacherName = s;
	}

	public ImageButtonBean getCancel() {
		return cancel;
	}

	public ImageButtonBean getSave() {
		return save;
	}

	public boolean getIsExisting() {
		return isExisting;
	}

	public void setIsExisting(boolean b) {
		isExisting = b;
	}

	/**
	 * This method is called by the RequestProcessor immediately after
	 * populating the TeacherForm only if the validate=true flag is set in the
	 * helloworld-config.xml Do validation here and return ActionErrors if any,
	 * to the RequestProcessor
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		// Need to do this explicitly now. Since the Button is not grey and its
		// name is not
		// org.apache.struts...Cancel anymore, and the validate=true still
		// remains in ActionMapping, the call to bypass validation on clicking
		// cancel image
		// has to be performed manually
		if (getCancel().isSelected()) {
			return errors;
		}

		MessageResources msgRes = (MessageResources) request
				.getAttribute(Globals.MESSAGES_KEY);

		// TeacherName cannot be empty
		if (teacherName == null || teacherName.trim().equals("")) {
			String teacherName = msgRes.getMessage("teacher.teachername");
			String[] rplcmntValueArr = { teacherName };
			ActionError err = new ActionError("error.required", rplcmntValueArr);
			errors.add("teacherName", err);
		}
		return errors;
	}

}